package org.example.Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2022-03-21
 * Time:12:10
 */
public class Demo7 {
    private static final long count = 10_0000_0000;
    public static void serial(){
        //记录程序执行时间
        long begin = System.currentTimeMillis();
        long a = 0;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0;
        for (int i = 0; i < count; i++) {
            b++;
        }
        //记录结束时间
        long end = System.currentTimeMillis();
        System.out.println("串行消耗的时间:" + (end - begin) + "ms");
    }

    public static void concurrency() throws InterruptedException {
        long start = System.currentTimeMillis();
        java.lang.Thread t1 = new java.lang.Thread(() ->{
            long a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        t1.start();
        java.lang.Thread t2 = new java.lang.Thread(() ->{
            long b = 0;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("并行消耗的时间:"+ (end - start)+"ms");
    }
    public static void main(String[] args) throws InterruptedException {
        serial();
        concurrency();
    }
}
